package com.dfy.config;

import com.dfy.dto.SpotFacilityRequest;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;

public class SpotFacilityRequestDeserializer extends JsonDeserializer<SpotFacilityRequest> {
    @Override
    public SpotFacilityRequest deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        SpotFacilityRequest request = new SpotFacilityRequest();
        
        if (node.isTextual()) {
            // 如果是简单字符串，将其同时设置为名称和值
            String value = node.asText();
            request.setFacilityName(value);
            request.setFacilityValue(value);
        } else if (node.isObject()) {
            // 如果是对象，分别获取 facility_name 和 facility_value
            request.setFacilityName(node.get("facility_name").asText());
            request.setFacilityValue(node.get("facility_value").asText());
        }
        
        return request;
    }
} 